﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:flow_node
	/// </summary>
	public partial class flow_node
	{
		public flow_node()
		{}
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("flow_node_id", "flow_node");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int flow_node_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from flow_node");
            strSql.Append(" where flow_node_id=@flow_node_id");
            SqlParameter[] parameters = {
					new SqlParameter("@flow_node_id", SqlDbType.Int,4)
			};
            parameters[0].Value = flow_node_id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Maticsoft.Model.flow_node model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into flow_node(");
            strSql.Append("flow_id,flow_node_num,flow_node_name,flow_node_isLast,flow_node_all,flow_node_allapprove,flow_node_preserving,flow_node_isMulti,flow_node_isAutoNext)");
            strSql.Append(" values (");
            strSql.Append("@flow_id,@flow_node_num,@flow_node_name,@flow_node_isLast,@flow_node_all,@flow_node_allapprove,@flow_node_preserving,@flow_node_isMulti,@flow_node_isAutoNext)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@flow_id", SqlDbType.Int,4),
					new SqlParameter("@flow_node_num", SqlDbType.TinyInt,1),
					new SqlParameter("@flow_node_name", SqlDbType.VarChar,50),
					new SqlParameter("@flow_node_isLast", SqlDbType.Bit,1),
					new SqlParameter("@flow_node_all", SqlDbType.TinyInt,1),
					new SqlParameter("@flow_node_allapprove", SqlDbType.TinyInt,1),
					new SqlParameter("@flow_node_preserving", SqlDbType.TinyInt,1),
					new SqlParameter("@flow_node_isMulti", SqlDbType.TinyInt,1),
					new SqlParameter("@flow_node_isAutoNext", SqlDbType.Bit,1)};
            parameters[0].Value = model.flow_id;
            parameters[1].Value = model.flow_node_num;
            parameters[2].Value = model.flow_node_name;
            parameters[3].Value = model.flow_node_isLast;
            parameters[4].Value = model.flow_node_all;
            parameters[5].Value = model.flow_node_allapprove;
            parameters[6].Value = model.flow_node_preserving;
            parameters[7].Value = model.flow_node_isMulti;
            parameters[8].Value = model.flow_node_isAutoNext;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Maticsoft.Model.flow_node model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update flow_node set ");
            strSql.Append("flow_id=@flow_id,");
            strSql.Append("flow_node_num=@flow_node_num,");
            strSql.Append("flow_node_name=@flow_node_name,");
            strSql.Append("flow_node_isLast=@flow_node_isLast,");
            strSql.Append("flow_node_all=@flow_node_all,");
            strSql.Append("flow_node_allapprove=@flow_node_allapprove,");
            strSql.Append("flow_node_preserving=@flow_node_preserving,");
            strSql.Append("flow_node_isMulti=@flow_node_isMulti,");
            strSql.Append("flow_node_isAutoNext=@flow_node_isAutoNext");
            strSql.Append(" where flow_node_id=@flow_node_id");
            SqlParameter[] parameters = {
					new SqlParameter("@flow_id", SqlDbType.Int,4),
					new SqlParameter("@flow_node_num", SqlDbType.TinyInt,1),
					new SqlParameter("@flow_node_name", SqlDbType.VarChar,50),
					new SqlParameter("@flow_node_isLast", SqlDbType.Bit,1),
					new SqlParameter("@flow_node_all", SqlDbType.TinyInt,1),
					new SqlParameter("@flow_node_allapprove", SqlDbType.TinyInt,1),
					new SqlParameter("@flow_node_preserving", SqlDbType.TinyInt,1),
					new SqlParameter("@flow_node_isMulti", SqlDbType.TinyInt,1),
					new SqlParameter("@flow_node_isAutoNext", SqlDbType.Bit,1),
					new SqlParameter("@flow_node_id", SqlDbType.Int,4)};
            parameters[0].Value = model.flow_id;
            parameters[1].Value = model.flow_node_num;
            parameters[2].Value = model.flow_node_name;
            parameters[3].Value = model.flow_node_isLast;
            parameters[4].Value = model.flow_node_all;
            parameters[5].Value = model.flow_node_allapprove;
            parameters[6].Value = model.flow_node_preserving;
            parameters[7].Value = model.flow_node_isMulti;
            parameters[8].Value = model.flow_node_isAutoNext;
            parameters[9].Value = model.flow_node_id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int flow_node_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from flow_node ");
            strSql.Append(" where flow_node_id=@flow_node_id");
            SqlParameter[] parameters = {
					new SqlParameter("@flow_node_id", SqlDbType.Int,4)
			};
            parameters[0].Value = flow_node_id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string flow_node_idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from flow_node ");
            strSql.Append(" where flow_node_id in (" + flow_node_idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.flow_node GetModel(int flow_node_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 flow_node_id,flow_id,flow_node_num,flow_node_name,flow_node_isLast,flow_node_all,flow_node_allapprove,flow_node_preserving,flow_node_isMulti,flow_node_isAutoNext from flow_node ");
            strSql.Append(" where flow_node_id=@flow_node_id");
            SqlParameter[] parameters = {
					new SqlParameter("@flow_node_id", SqlDbType.Int,4)
			};
            parameters[0].Value = flow_node_id;

            Maticsoft.Model.flow_node model = new Maticsoft.Model.flow_node();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.flow_node DataRowToModel(DataRow row)
        {
            Maticsoft.Model.flow_node model = new Maticsoft.Model.flow_node();
            if (row != null)
            {
                if (row["flow_node_id"] != null && row["flow_node_id"].ToString() != "")
                {
                    model.flow_node_id = int.Parse(row["flow_node_id"].ToString());
                }
                if (row["flow_id"] != null && row["flow_id"].ToString() != "")
                {
                    model.flow_id = int.Parse(row["flow_id"].ToString());
                }
                if (row["flow_node_num"] != null && row["flow_node_num"].ToString() != "")
                {
                    model.flow_node_num = int.Parse(row["flow_node_num"].ToString());
                }
                if (row["flow_node_name"] != null)
                {
                    model.flow_node_name = row["flow_node_name"].ToString();
                }
                if (row["flow_node_isLast"] != null && row["flow_node_isLast"].ToString() != "")
                {
                    if ((row["flow_node_isLast"].ToString() == "1") || (row["flow_node_isLast"].ToString().ToLower() == "true"))
                    {
                        model.flow_node_isLast = true;
                    }
                    else
                    {
                        model.flow_node_isLast = false;
                    }
                }
                if (row["flow_node_all"] != null && row["flow_node_all"].ToString() != "")
                {
                    model.flow_node_all = int.Parse(row["flow_node_all"].ToString());
                }
                if (row["flow_node_allapprove"] != null && row["flow_node_allapprove"].ToString() != "")
                {
                    model.flow_node_allapprove = int.Parse(row["flow_node_allapprove"].ToString());
                }
                if (row["flow_node_preserving"] != null && row["flow_node_preserving"].ToString() != "")
                {
                    model.flow_node_preserving = int.Parse(row["flow_node_preserving"].ToString());
                }
                if (row["flow_node_isMulti"] != null && row["flow_node_isMulti"].ToString() != "")
                {
                    model.flow_node_isMulti = int.Parse(row["flow_node_isMulti"].ToString());
                }
                if (row["flow_node_isAutoNext"] != null && row["flow_node_isAutoNext"].ToString() != "")
                {
                    if ((row["flow_node_isAutoNext"].ToString() == "1") || (row["flow_node_isAutoNext"].ToString().ToLower() == "true"))
                    {
                        model.flow_node_isAutoNext = true;
                    }
                    else
                    {
                        model.flow_node_isAutoNext = false;
                    }
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select flow_node_id,flow_id,flow_node_num,flow_node_name,flow_node_isLast,flow_node_all,flow_node_allapprove,flow_node_preserving,flow_node_isMulti,flow_node_isAutoNext ");
            strSql.Append(" FROM flow_node ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" flow_node_id,flow_id,flow_node_num,flow_node_name,flow_node_isLast,flow_node_all,flow_node_allapprove,flow_node_preserving,flow_node_isMulti,flow_node_isAutoNext ");
            strSql.Append(" FROM flow_node ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM flow_node ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.flow_node_id desc");
            }
            strSql.Append(")AS Row, T.*  from flow_node T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "flow_node";
            parameters[1].Value = "flow_node_id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

